package com.scrx.ymg.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class LatLonUtil {
	private static final double PI = 3.14159265;  
    private static final double EARTH_RADIUS = 6378137;  
    private static final double RAD = Math.PI / 180.0;  
  
    //@see http://snipperize.todayclose.com/snippet/php/SQL-Query-to-Find-All-Retailers-Within-a-Given-Radius-of-a-Latitude-and-Longitude--65095/   
    //The circumference of the earth is 24,901 miles.  
    //24,901/360 = 69.17 miles / degree    
    /** 
     * @param raidus 单位米 
     * return minLat,minLng,maxLat,maxLng 
     */  
    public static double[] getAround(double lat,double lon,int raidus){  
          
        Double latitude = lat;  
        Double longitude = lon;  
          
        Double degree = (24901*1609)/360.0;  
        double raidusMile = raidus;  
          
        Double dpmLat = 1/degree;  
        Double radiusLat = dpmLat*raidusMile;  
        Double minLat = latitude - radiusLat;  
        Double maxLat = latitude + radiusLat;  
          
        Double mpdLng = degree*Math.cos(latitude * (PI/180));  
        Double dpmLng = 1 / mpdLng;  
        Double radiusLng = dpmLng*raidusMile;  
        Double minLng = longitude - radiusLng;  
        Double maxLng = longitude + radiusLng;  
        //System.out.println("["+minLat+","+minLng+","+maxLat+","+maxLng+"]");  
        
        return new double[]{maxLng,maxLat,minLng,minLat};  
    }  
      
    /** 
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米 
     * @param lng1 
     * @param lat1 
     * @param lng2 
     * @param lat2 
     * @return 
     */  
    public static double getDistance(double lng1, double lat1, double lng2, double lat2)  
    {  
       double radLat1 = lat1*RAD;  
       double radLat2 = lat2*RAD;  
       double a = radLat1 - radLat2;  
       double b = (lng1 - lng2)*RAD;  
       double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +  
        Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));  
       s = s * EARTH_RADIUS;  
       s = Math.round(s * 10000) / 10000;  
       return s;  
    }  
      
    public static void main(String[] args){  
        Double lat1 = 34.264648;  
        Double lon1 = 108.952736;  
          
        int radius = 1000;  
        //[34.25566276027792,108.94186385411045,34.27363323972208,108.96360814588955]  
        getAround(lat1,lon1,radius);  
          
        //911717.0   34.264648,108.952736,39.904549,116.407288  
        double dis = getDistance(108.952736,34.264648,116.407288,39.904549);   
        System.out.println(dis);  
    }
    
    /**
     * 根据参数拼接hql查询渔区
     * @param obj 值
     * @param obj2 字段
     * @param hql 查询语句拼接
     */
    public void init(Object obj,Object obj2,StringBuffer hql){
		  if(obj2.equals("userName")&&obj!=null&obj!=""){
			  hql.append(" and "+obj2+" like '%"+obj+"%'");
		  }else if(obj2.equals("superName")&&obj!=null&obj!=""){
			  hql.append(" and "+obj2+" like '%"+obj+"%'");
		  }else if(obj2.equals("name")&&obj!=null&obj!=""){
			  hql.append(" and "+obj2+" like '%"+obj+"%'");
		  }else if(obj2.equals("storeName")&&obj!=null&obj!=""){
			  hql.append(" and "+obj2+" like '%"+obj+"%'");
		  }else if(obj2.equals("productName")&&obj!=null&obj!=""){
			  hql.append(" and "+obj2+" like '%"+obj+"%'");
		  }else if(obj2.equals("otherName")&&obj!=null&obj!=""){
			  hql.append(" and "+obj2+" like '%"+obj+"%'");
		  }else if(obj2.equals("directName")&&obj!=null&obj!=""){
			  hql.append(" and "+obj2+" like '%"+obj+"%'");
		  }else if(obj!=null&&obj!=""){
				hql.append(" and "+obj2+"='"+obj+"'");
		  }
	  }
    /**
     * 根据参数拼接hql查询语句
     * @param obj 值
     * @param obj2 字段
     * @param hql 查询语句拼接
     */
    public void initStatus(Object obj,Object obj2,StringBuffer hql){
          if(obj!=null&&obj!=""&&Integer.parseInt(obj.toString())==-1){
        	  hql.append(" and "+obj2+" in('0','1')");
          }else if(obj!=null&&obj!=""&&Integer.parseInt(obj.toString())==-2){
        	  hql.append(" and "+obj2+" in('2','4')");
          }else if(obj!=null&&obj!=""&&Integer.parseInt(obj.toString())==-3){
        	  hql.append(" and "+obj2+" in('0','2')");
          }else if(obj!=null&&obj!=""&&Integer.parseInt(obj.toString())==-4){
        	  hql.append(" and "+obj2+" in('1','3','4')");
          }else if(obj!=null&&obj!=""&&Integer.parseInt(obj.toString())==-5){
        	  hql.append(" and "+obj2+" in('1','5')");
          }else if(obj!=null&&obj!=""&&Integer.parseInt(obj.toString())==-6){
        	  hql.append(" and "+obj2+" in('1','3')");
          }else if(obj!=null&&obj!=""){
				hql.append(" and "+obj2+"='"+obj+"'");
		  }
	  }
    public void initStartDateTime(Object time,Object obj,StringBuffer hql){
		  if(time!=null&&time!=""){
			  hql.append(" and "+obj+">='"+time+"'");
		  }
	  }
    public void initEndDateTime(Object time,Object obj,StringBuffer hql){
		  if(time!=null&&time!=""){
			  hql.append(" and "+obj+"<='"+time+"'");
		  }
	  }
    
    public String newdate(){
    	SimpleDateFormat  formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
    	return formatter.format(new Date());
    }
    public String newdateNoTime(){
    	SimpleDateFormat  formatter = new SimpleDateFormat ("yyyy-MM-dd");
    	return formatter.format(new Date());
    }

}
